package com.polaris.live.popup

import android.content.Context
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.manager.CoroutineScopeManager
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.PopupWeekStarBlockBinding
import com.polaris.live.net.liveService
import com.polaris.live.resp.go_resp.UserIdBean
import kotlinx.coroutines.launch

/**
 * WeekStarBlockPopup 周星拉黑
 * @author Created by 天晴 on 2024/5/13 10:31
 * @since 1.0.0
 **/
class WeekStarBlockPopup(
    context: Context,
    val userId: Long,
    val close: (() -> Unit)?,
) : CenterPopupView(context) {

    private lateinit var mBinding: PopupWeekStarBlockBinding
    override fun getImplLayoutId(): Int {
        return com.polaris.live.R.layout.popup_week_star_block
    }

    override fun onCreate() {
        super.onCreate()

        mBinding = PopupWeekStarBlockBinding.bind(popupImplView)

        mBinding.tvClose.setOnSingleClickListener {
            addRejectList()
            close?.invoke()
            dismiss()
        }

        mBinding.tvOk.setOnSingleClickListener {
            close?.invoke()
            dismiss()
        }
    }

    private fun addRejectList() {
        CoroutineScopeManager.defaultScope.launch {
            val postResp = liveService.addRejectList(UserIdBean(userId))
            if (!postResp.isSuccess()) {
                ToastGravity.showGravity(postResp.getResponseMsg())
            }
        }
    }

    companion object {
        fun showPop(
            context: Context,
            userId: Long,
            close: (() -> Unit)? = null,
        ) {
            XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .hasShadowBg(true)
                .asCustom(WeekStarBlockPopup(context, userId, close))
                .show()
        }
    }

}